<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Category;
use app\common\model\Events;
use app\common\model\Goods;
use app\common\model\MoneyLog;
use app\common\model\order\Order;
use app\common\model\User;
use think\Db;

/**
 * 商家接口
 */
class Shop extends Api
{
    protected $noNeedLogin = ['lists'];
    protected $noNeedRight = ['*'];

    /**
     * @Author LK
     * @ApiTitle (门店数据)
     * @ApiMethod (get)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     * 
     */
    public function index()
    {
        $total_fee = Order::where('shop_id', $this->auth->id)->whereTime('paytime', 'month')->whereIn('pay_type', ['1', '2'])->whereNotIn('order_status', ['0', '-1'])->sum('total_fee');
        $total_point = Order::where('shop_id', $this->auth->id)->whereTime('paytime', 'month')->where('pay_type', 4)->whereNotIn('order_status', ['0', '-1'])->sum('total_points');
        $total_num = Order::where('shop_id', $this->auth->id)->whereTime('paytime', 'month')->where('pay_type', 5)->whereNotIn('order_status', ['0', '-1'])->sum('num');
        $total_count = Order::where('shop_id', $this->auth->id)->whereTime('paytime', 'month')->whereNotIn('order_status', ['0', '-1'])->count();
        $wait_count = Order::where('shop_id', $this->auth->id)->where('order_type', '1')->where('order_status', '10')->count();
        $cleaning_count = Order::where('shop_id', $this->auth->id)->where('order_status', '20')->count();
        $distribution_count = Order::where('shop_id', $this->auth->id)->where('order_status', '40')->count();
        $this->success('请求成功', compact('total_fee', 'total_point', 'total_num', 'total_count', 'wait_count', 'cleaning_count', 'distribution_count'));
    }

    /**
     * @Author LK
     * @ApiTitle (商家列表)
     * @ApiMethod (get)
     * @ApiParams (name="city", type="string", required=true, sample="", description="所在城市商家")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function lists()
    {
        $param = request()->param();
        extract($param);
        $shop = new User();
        if (isset($city) && $city) {
            $shop->whereLike('city', '%' . $city . '%');
        }
        $shop_user = $shop->where('group_id', 2)->find();
        $this->success('请求成功', $shop_user);
    }

    /**
     * @Author LK
     * @ApiTitle (商家订单)
     * @ApiMethod (get)
     * @ApiParams (name="order_status", type="integer", required=true, sample="", description="all=全部,10=待取鞋/待自送,15=清洁中,20=清洁中,30=待配送,40=配送中")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function shop_order()
    {
        $param = request()->param();
        extract($param);
        $order = new Order;
        if ($order_status != 'all') {
            if ($order_status == '40') {
                $order->whereIn('order_status', ['40', '50', '60']);
            } else {
                $order->where('order_status', $order_status);
            }
        }
        $list = $order->where('order_status', '>', '0')->where('shop_id', $this->auth->id)->order('id desc')->paginate();
        $this->success('请求成功', $list);
    }
    /**
     * @Author LK
     * @ApiTitle (订单信息编辑)
     * @ApiMethod (POST)
     * @ApiParams (name="type", type="string", required=true, sample="", description="1=取货员，2=配送员")
     * @ApiParams (name="uid", type="string", required=true, sample="", description="id")
     * @ApiParams (name="order_id", type="string", required=true, sample="", description="订单id")
     * @ApiParams (name="address", type="string", required=true, sample="", description="收货地址")
     * @ApiParams (name="contact", type="string", required=true, sample="", description="收货人")
     * @ApiParams (name="telephone", type="string", required=true, sample="", description="联系方式")
     * @ApiParams (name="address_lng", type="string", required=true, sample="", description="收货地址经度")
     * @ApiParams (name="address_lat", type="string", required=true, sample="", description="收货地址纬度")
     * @ApiParams (name="clean_images", type="string", required=true, sample="", description="清洗照片")
     * @ApiParams (name="mark", type="string", required=true, sample="", description="备注")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function clerk()
    {
        $param = request()->param();
        extract($param);
        $time = time();
        Db::startTrans();
        try {
            $order = Order::get($order_id);
            if (isset($uid) && $uid) {
                if ($type == 1) {
                    $order->take_id = $uid;
                } else {
                    $order->send_id = $uid;
                }
            }
            $order->contact = $contact ?? '';
            $order->telephone = $telephone ?? '';
            $order->city = $city ?? '';
            $order->address = $address ?? '';
            $order->address_lng = $address_lng ?? '';
            $order->address_lat = $address_lat ?? '';
            $order->mark = $mark ?? '';
            if (isset($clean_images) && $clean_images) {
                $order->clean_images = $clean_images;
                $order->cleantime = $time;
            }
            $order->save();
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('提交成功');
    }
    /**
     * @Author LK
     * @ApiTitle (门店操作)
     * @ApiMethod (POST)
     * @ApiParams (name="order_id", type="integer", required=true, sample="", description="ID")
     * @ApiParams (name="take_code", type="string", required=true, sample="", description="取鞋码")
     * @ApiParams (name="status", type="integer", required=true, sample="", description="10=确认收到货,15=开始清洗,20=清洗完成,30=订单完成")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function controls()
    {
        $param = request()->param();
        extract($param);
        $time =time();
        $order = Order::get($order_id);
        if (!$order) {
            $this->error('订单不存在');
        }
        if ($status == 10) {
            if ($take_code != $order->take_code) {
                $this->error('取鞋码错误');
            }
            $order->order_status = '15';
        } elseif ($status == 15) {
            $order->order_status = '20';
            $order->cleantime = $time;
        } elseif ($status == 20) {
            $order->order_status = '30';
        } elseif ($status == 30) {
            if ($take_code != $order->take_code) {
                $this->error('取鞋码错误');
            }
            MoneyLog::subcommission($order->user_id, $order->orderid);
            $order->order_status = '50';
            $order->sendtime = $time;
        }

        $res = $order->save();
        if ($res) {
            $this->success('提交成功');
        }
        $this->error('提交失败');
    }
}
